﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace DjCommon
{
    public class Utils
    {
        /// <summary>
        /// 根据文章ID得到重写的url
        /// </summary>
        public static string GetArticleUrl(int articleID)
        {
            if (articleID > 0)
            {
                return string.Format("/post/{0}.html", articleID);
            }
            else
            {
                return "/";
            }
        }

        /// <summary>
        /// 得到分类的重写url
        /// </summary>
        /// <param name="termid"></param>
        /// <returns></returns>
        public static string GetTermUrl(int termid)
        {
            if (termid > 0)
            {
                return string.Format("/cat/{0}.html", termid);
            }
            else
            {
                return "/";
            }
        }

        /// <summary>
        /// 得到标签的重写url
        /// </summary>
        /// <param name="termid"></param>
        /// <returns></returns>
        public static string GetTagUrl(int tagid)
        {
            if (tagid > 0)
            {
                return string.Format("/tag/{0}.html", tagid);
            }
            else
            {
                return "/";
            }
        }

        /// <summary>
        /// 得到月份简写
        /// </summary>
        /// <param name="month"></param>
        /// <returns></returns>
        public static string GetMonthSim(int month)
        {
            string[] months = { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" };
            if (month > 0 && month < 13)
            {
                return months[month-1];
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string GetPostContentNoMore(string content)
        {
            content = HttpContext.Current.Server.HtmlDecode(content);
            int i = content.IndexOf("<!--more-->");
            if (i < 0)
            {
                return content;
            }
            else
            {
                return content.Substring(0, i);
            }
        }

        


        /// <summary>
        /// 判断当前页面是否接收到了Post请求
        /// </summary>
        /// <returns>是否接收到了Post请求</returns>
        public static bool IsPost
        {
            get
            {
                return HttpContext.Current.Request.HttpMethod.Equals("POST");
            }
        }

        /// <summary>
        /// 判断当前页面是否接收到了Get请求
        /// </summary>
        /// <returns>是否接收到了Get请求</returns>
        public static bool IsGet
        {
            get
            {
                return HttpContext.Current.Request.HttpMethod.Equals("GET");
            }
        }

        /// <summary>
        /// 返回上一个页面的地址
        /// </summary>
        /// <returns>上一个页面的地址</returns>
        public static string GetUrlReferrer()
        {
            string retVal = null;

            try
            {
                retVal = HttpContext.Current.Request.UrlReferrer.ToString();
            }
            catch { }

            if (retVal == null)
                return "";

            return retVal;
        }

        /// <summary>
        /// 返回指定的服务器变量信息
        /// </summary>
        /// <param name="strName">服务器变量名</param>
        /// <returns>服务器变量信息</returns>
        public static string GetServerString(string strName)
        {
            if (HttpContext.Current.Request.ServerVariables[strName] == null)
                return "";

            return HttpContext.Current.Request.ServerVariables[strName].ToString();
        }

        /// <summary>
        /// 得到当前完整主机头
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentFullHost()
        {
            HttpRequest request = System.Web.HttpContext.Current.Request;
            if (!request.Url.IsDefaultPort)
                return string.Format("{0}:{1}", request.Url.Host, request.Url.Port.ToString());

            return request.Url.Host;
        }

        /// <summary>
        /// 得到主机头
        /// </summary>
        /// <returns></returns>
        public static string GetHost()
        {
            return HttpContext.Current.Request.Url.Host;
        }

        /// <summary>
        /// 获取当前请求的原始 URL(URL 中域信息之后的部分,包括查询字符串(如果存在))
        /// </summary>
        /// <returns>原始 URL</returns>
        public static string GetRawUrl()
        {
            return HttpContext.Current.Request.RawUrl;
        }

        /// <summary>
        /// 获得当前完整Url地址
        /// </summary>
        /// <returns>当前完整Url地址</returns>
        public static string GetUrl()
        {
            return HttpContext.Current.Request.Url.ToString();
        }

        /// <summary>
        /// 获取session
        /// </summary>
        /// <param name="sname"></param>
        /// <returns></returns>
        public static object GetSession(string sname)
        {
            return HttpContext.Current.Session[sname];
        }

        /// <summary>
        /// 设置session
        /// </summary>
        /// <param name="sname"></param>
        /// <param name="val"></param>
        public static void SetSession(string sname, object val)
        {
            HttpContext.Current.Session[sname] = val;
        }

        /// <summary>
        /// 移除session
        /// </summary>
        /// <param name="sname"></param>
        public static void RemoveSession(string sname)
        {
            HttpContext.Current.Session.Remove(sname);
        }

        /// <summary>
        /// 获取cookie
        /// </summary>
        /// <param name="cname"></param>
        /// <returns></returns>
        public static string GetCookie(string cname)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cname];

            if (cookie != null)
            {
                return cookie.Value;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="cname"></param>
        /// <param name="val"></param>
        public static void SetCookie(string cname, string val)
        {
            HttpCookie cookie = new HttpCookie(cname, val);
            cookie.Expires = DateTime.Now.AddDays(30);
            HttpContext.Current.Response.Cookies.Set(cookie);
        }

        /// <summary>
        /// 移除cookie
        /// </summary>
        /// <param name="cname"></param>
        public static void RemoveCookie(string cname)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cname];

            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }

        /// <summary>
        /// 获得指定Url参数的值
        /// </summary>
        /// <param name="strName">Url参数</param>
        /// <returns>Url参数的值</returns>
        public static string GetQueryString(string strName)
        {
            if (HttpContext.Current.Request.QueryString[strName] == null)
                return "";

            return HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.QueryString[strName].Trim());
        }

        /// <summary>
        /// 获得指定表单参数的值
        /// </summary>
        /// <param name="strName">表单参数</param>
        /// <returns>表单参数的值</returns>
        public static string GetFormString(string strName)
        {
            if (HttpContext.Current.Request.Form[strName] == null)
                return "";

            return HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Form[strName].Trim());
        }

        /// <summary>
        /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName">参数</param>
        /// <returns>Url或表单参数的值</returns>
        public static string GetString(string strName)
        {
            if (HttpContext.Current.Request[strName] == null)
                return "";
            string s = HttpContext.Current.Request[strName].Trim();

            return HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request[strName].Trim());
        }

        /// <summary>
        /// 获得指定Url参数的int类型值
        /// </summary>
        /// <param name="strName">Url参数</param>
        /// <returns>Url参数的int类型值</returns>
        public static int GetQueryInt(string strName)
        {
            string s = HttpContext.Current.Request.QueryString[strName];
            int ret = 0;
            int.TryParse(s, out ret);
            return ret;
        }

        /// <summary>
        /// 获得指定Url参数的int类型值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="defVal"></param>
        /// <returns></returns>
        public static int GetQueryInt(string strName, int defVal)
        {
            string s = HttpContext.Current.Request.QueryString[strName];
            int ret = defVal;
            if (!int.TryParse(s, out ret))
            {
                ret = defVal;
            }
            return ret;
        }

        /// <summary>
        /// 获得指定表单参数的int类型值
        /// </summary>
        /// <param name="strName">表单参数</param>
        /// <returns>表单参数的int类型值</returns>
        public static int GetFormInt(string strName)
        {
            string s = HttpContext.Current.Request.Form[strName];
            int ret = 0;
            int.TryParse(s, out ret);
            return ret;
        }

        /// <summary>
        /// 获得指定表单参数的int类型值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="defVal"></param>
        /// <returns></returns>
        public static int GetFormInt(string strName, int defVal)
        {
            string s = HttpContext.Current.Request.Form[strName];
            int ret = defVal;
            if (!int.TryParse(s, out ret))
            {
                ret = defVal;
            }
            return ret;
        }

        /// <summary>
        /// 获得指定Url或表单参数的int类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName">Url或表单参数</param>
        /// <returns>Url或表单参数的int类型值</returns>
        public static int GetInt(string strName)
        {
            string s = HttpContext.Current.Request[strName];
            int ret = 0;
            int.TryParse(s, out ret);
            return ret;
        }

        /// <summary>
        /// 获得指定Url或表单参数的int类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="defVal"></param>
        /// <returns></returns>
        public static int GetInt(string strName, int defVal)
        {
            string s = HttpContext.Current.Request[strName];
            int ret = defVal;
            if (!int.TryParse(s, out ret))
            {
                ret = defVal;
            }
            return ret;
        }

        /// <summary>
        /// 获得指定Url或表单参数的double类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName"></param>
        /// <returns></returns>
        public static double GetQueryDouble(string strName)
        {
            string s = HttpContext.Current.Request.QueryString[strName];
            double ret = 0;
            double.TryParse(s, out ret);
            return ret;
        }

        /// <summary>
        /// 获得指定Url或表单参数的double类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="defVal"></param>
        /// <returns></returns>
        public static double GetQueryDouble(string strName, double defVal)
        {
            string s = HttpContext.Current.Request.QueryString[strName];
            double ret = defVal;
            if (!double.TryParse(s, out ret))
            {
                ret = defVal;
            }
            return ret;
        }

        /// <summary>
        /// 获得指定Url或表单参数的double类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName"></param>
        /// <returns></returns>
        public static double GetFormDouble(string strName)
        {
            string s = HttpContext.Current.Request.Form[strName];
            double ret = 0;
            double.TryParse(s, out ret);
            return ret;
        }

        /// <summary>
        /// 获得指定Url或表单参数的double类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="defVal"></param>
        /// <returns></returns>
        public static double GetFormDouble(string strName, double defVal)
        {
            string s = HttpContext.Current.Request.Form[strName];
            double ret = defVal;
            if (!double.TryParse(s, out ret))
            {
                ret = defVal;
            }
            return ret;
        }

        /// <summary>
        /// 获得指定Url或表单参数的double类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName"></param>
        /// <returns></returns>
        public static double GetDouble(string strName)
        {
            string s = HttpContext.Current.Request[strName];
            double ret = 0;
            double.TryParse(s, out ret);
            return ret;
        }

        /// <summary>
        /// 获得指定Url或表单参数的double类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="defVal"></param>
        /// <returns></returns>
        public static double GetDouble(string strName, double defVal)
        {
            string s = HttpContext.Current.Request[strName];
            double ret = defVal;
            if (!double.TryParse(s, out ret))
            {
                ret = defVal;
            }
            return ret;
        }

        /// <summary>
        /// 获得指定Url参数的long类型值
        /// </summary>
        /// <param name="strName">Url参数</param>
        /// <returns>Url参数的long类型值</returns>
        public static long GetQueryLong(string strName)
        {
            string s = HttpContext.Current.Request.QueryString[strName];
            long ret = 0;
            long.TryParse(s, out ret);
            return ret;
        }

        /// <summary>
        /// 获得指定Url参数的long类型值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="defVal"></param>
        /// <returns></returns>
        public static long GetQueryLong(string strName, long defVal)
        {
            string s = HttpContext.Current.Request.QueryString[strName];
            long ret = defVal;
            if (!long.TryParse(s, out ret))
            {
                ret = defVal;
            }
            return ret;
        }

        /// <summary>
        /// 获得指定表单参数的long类型值
        /// </summary>
        /// <param name="strName">表单参数</param>
        /// <returns>表单参数的long类型值</returns>
        public static long GetFormLong(string strName)
        {
            string s = HttpContext.Current.Request.Form[strName];
            long ret = 0;
            long.TryParse(s, out ret);
            return ret;
        }

        /// <summary>
        /// 获得指定表单参数的long类型值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="defVal"></param>
        /// <returns></returns>
        public static long GetFormLong(string strName, long defVal)
        {
            string s = HttpContext.Current.Request.Form[strName];
            long ret = defVal;
            if (!long.TryParse(s, out ret))
            {
                ret = defVal;
            }
            return ret;
        }

        /// <summary>
        /// 获得指定Url或表单参数的long类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName">Url或表单参数</param>
        /// <returns>Url或表单参数的long类型值</returns>
        public static long GetLong(string strName)
        {
            string s = HttpContext.Current.Request[strName];
            long ret = 0;
            long.TryParse(s, out ret);
            return ret;
        }

        /// <summary>
        /// 获得指定Url或表单参数的long类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="defVal"></param>
        /// <returns></returns>
        public static long GetLong(string strName, long defVal)
        {
            string s = HttpContext.Current.Request[strName];
            long ret = defVal;
            if (!long.TryParse(s, out ret))
            {
                ret = defVal;
            }
            return ret;
        }

        /// <summary>
        /// 获得当前页面的名称
        /// </summary>
        /// <returns>当前页面的名称</returns>
        public static string GetPageName()
        {
            string[] urlArr = HttpContext.Current.Request.Url.AbsolutePath.Split('/');
            return urlArr[urlArr.Length - 1].ToLower();
        }

        private static readonly Regex MOBILE_REGEX = new Regex(@"(nokia|sonyericsson|blackberry|samsung|sec\-|windows ce|motorola|mot\-|up.b|midp\-)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        /// <summary>
        /// 是否是移动设备
        /// </summary>
        public static bool IsMobile
        {
            get
            {
                HttpContext context = HttpContext.Current;
                if (context != null)
                {
                    HttpRequest request = context.Request;
                    if (request.Browser.IsMobileDevice)
                        return true;

                    if (!string.IsNullOrEmpty(request.UserAgent) && MOBILE_REGEX.IsMatch(request.UserAgent))
                        return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 客户端IP
        /// </summary>
        /// <returns></returns>
        public static string ClientIP
        {
            get
            {
                string result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                if (string.IsNullOrEmpty(result))
                    result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

                if (string.IsNullOrEmpty(result))
                    result = HttpContext.Current.Request.UserHostAddress;

                if (string.IsNullOrEmpty(result))
                    return "127.0.0.1";

                return result;
            }
        }


        /// <summary>
        /// 是否来自爬虫
        /// </summary>
        /// <returns></returns>
        public static bool IsCrawler()
        {
            bool isCrawler = HttpContext.Current.Request.Browser.Crawler;

            if (!isCrawler)
            {
                Regex r = new Regex("google|baidu|yahoo|yisou|live|tom|163|iask|soso|sogou|zhongsou|3721|openfind|alltheweb|lycos|onseek|youdao|bing", RegexOptions.IgnoreCase);
                isCrawler = r.Match(HttpContext.Current.Request.UserAgent.ToLower()).Success;
            }

            return isCrawler;
        }
    }
}
